package com.hbt.orderfood.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.hbt.orderfood.R;
import com.hbt.orderfood.adapter.UserOrderAdapter;
import com.hbt.orderfood.db.DBAdapter;
import com.hbt.orderfood.entitys.Food;
import com.hbt.orderfood.entitys.Order;
import com.hbt.orderfood.utils.StringUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MyOrderActivity extends AppCompatActivity {

    @BindView(R.id.recycle)
    RecyclerView recycle;
    @BindView(R.id.submit)
    Button submit;
    @BindView(R.id.cancel)
    Button cancel;
    @BindView(R.id.total_price)
    TextView totalPrice;
    @BindView(R.id.has_submit_btn)
    TextView hasSubmitBtn;
    private UserOrderAdapter userOrderAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_order);
        ButterKnife.bind(this);
        initRecycler();
        setTotalPrice();
    }

    void initRecycler() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recycle.setLayoutManager(layoutManager);
        userOrderAdapter = new UserOrderAdapter();
        recycle.setAdapter(userOrderAdapter);
        userOrderAdapter.setListener(new UserOrderAdapter.OnClickAddSubListener() {
            @Override
            public void clickAddBtn(Food food) {
                for (Order o : StringUtils.orders) {
                    if (o.getFood_id() == food.getId()) {
                        o.setNum(o.getNum() + 1);
                        o.setPrice(o.getPrice() + food.getPrice());
                    }
                }
                setTotalPrice();
            }

            @Override
            public void clickSubBtn(Food food) {
                for (Order o : StringUtils.orders) {
                    if (o.getFood_id() == food.getId()) {
                        o.setNum(o.getNum() - 1);
                        o.setPrice(o.getPrice() - food.getPrice());
                    }
                }
                setTotalPrice();
            }
        });

    }//initRecycler

    void setTotalPrice() {
        totalPrice.setText("总计：" + String.valueOf(getTotalPrice()) + "￥");
    }

    int getTotalPrice() {
        int price = 0;
        for (Order o : StringUtils.orders) {
            price += o.getPrice();
        }
        return price;
    }

    @OnClick({R.id.submit, R.id.cancel})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.submit:
                new DBAdapter(MyOrderActivity.this).insertAllOrders(StringUtils.orders);
                StringUtils.orders.removeAll(StringUtils.orders);
                recycle.getAdapter().notifyDataSetChanged();
                setTotalPrice();
                Toast.makeText(MyOrderActivity.this, "订单提交成功", Toast.LENGTH_LONG).show();
                break;
            case R.id.cancel:
                StringUtils.orders.removeAll(StringUtils.orders);
                recycle.getAdapter().notifyDataSetChanged();
                setTotalPrice();
                break;
        }
    }


    @OnClick(R.id.has_submit_btn)
    public void onViewClicked() {
        Intent intent = new Intent(MyOrderActivity.this, MySubmitOrderActivity.class);
        startActivity(intent);
    }
}
